package com.halflife.nio2;

import io.netty.util.CharsetUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

public class BioSocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9090);
        Socket socket = null;
        while ((socket = serverSocket.accept()) != null) {
            final Socket currentSocket = socket;
            System.out.println("accept socket :"+socket.getRemoteSocketAddress());
            Thread thread = new Thread(() -> {
                try {
                    InputStream inputStream = currentSocket.getInputStream();
                    byte[] bytes = new byte[409600];
                    int len;
                    while ((len = inputStream.read(bytes)) > -1) {
                        System.out.println(new String(bytes,0,len, CharsetUtil.UTF_8));
                        System.out.println("END.........");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            },"Thread-"+socket.getRemoteSocketAddress().toString());
            thread.start();

        }
    }
}
